require 'extract/rules/xml_rule'
require 'singleton'
require 'enum'


module Extract
  
  class TwitterRules < Enum
    
    public
    
    #define de Brightkite extractions rules
    
    class Fullname < XMLRule
      def xpath
        "/user/name[text()]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.full_name = val
      end
    end
    
    class FirstName < Fullname
      protected
      
      def preproces_val(val)
        d = val.match /(\w+) (\w+)/
        val.replace d.nil? ? "" : d[1]
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.first_name = val.eql?("") ? nil : val
      end
    end
    
    class LastName < Fullname
      
      protected
      
      def preproces_val(val)
        d = val.match /(\w+) (\w+)/
        val.replace d.nil? ? "" : d[2]
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.last_name = val.eql?("") ? nil : val
      end
    end
    
    class FullLocations < XMLRule
      def xpath
        "/user/location[text()]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.full_location = val
      end
    end
    
    class Lang < XMLRule
      def xpath
        "/user/lang[text()]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.locale_language = val
      end
    end
    
    
    protected
    
    def initialize
      super()
      @collections[:Fullname] = Fullname.instance
      @collections[:LastName] = LastName.instance
      @collections[:FirstName] = FirstName.instance
      @collections[:FullLocations] = FullLocations.instance
      @collections[:Lang] = Lang.instance
    end
    
  end
  
end